#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <iostream>
#include <linux/if_ether.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <stropts.h>
#include <sys/ioctl.h>
#include "definitions.hpp"

using namespace std;

#define SIZE_ETHERNET 14


void* rdwr_thread_func(void* x){

 FILE *outFile = fopen("Output","ab");

 int i = 0;
 time_t mytime;
 while(1){
	 if(packet_q.size() <= 0){
	 usleep(100);
		 continue;
	 }
  if(i == 0)
  {
   mytime = time(NULL); /* not 'long' */
   i++;
  }
  unsigned char* buf = packet_q.front();
  packet_q.pop_front();
  
  
  // Read the raw message // Begin

	uint16_t size;
    FILE *outFile = fopen("Output","ab");
    bzero(&size,2); 
	memcpy(&size,buf+14,2);   
    cout<< "size : "  <<size<<endl;

	//Write data to file    
    fwrite (buf+16 , 1 , size , outFile );
	fflush(outFile);
	time_t mytime1 = time(NULL); /* not 'long' */
    printf("end time: %s", ctime(&mytime1));
    printf("start time: %s", ctime(&mytime));
	fclose(outFile);

	//free(buf);

}// while(1)

fclose(outFile);

pthread_exit(NULL);
}//rdwr function



